package com.jun.handler;

import com.jun.pojo.vo.R;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class BaseControllerAdvice {

  @ExceptionHandler({
      BindException.class
  })
  public R handler1(BindException e) {
    Map<String, String> ex = new HashMap<>();
    e.getBindingResult().getFieldErrors().forEach(i -> ex.put(i.getField(), i.getDefaultMessage()));
    return new R(500, "", ex);
  }

  @ExceptionHandler
  public R handler(Exception e) {
    e.printStackTrace();
    return new R().error();
  }

}
